package com.ouyang.bo.api.controller;

import com.ouyang.bo.api.util.Const;
import com.ouyang.bo.api.util.JSONResult;
import com.ouyang.bo.api.util.TokenUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 用户登录类
 *
 * @param
 * @author duanpj
 * @date 2020/8/4
 */
@RestController
@RequestMapping("/api/login")
public class ApiLoginController {

    private static final Logger logger = LoggerFactory.getLogger(ApiDuanpjController.class);

    /**
     * 用户登录接口
     *
     * @author duanpj
     * @date 2020/3/14 17:17
     */
    @PostMapping("/userLogin")
    @ResponseBody
    public JSONResult login() {
        try {
            Map<String, Object> resMap = new HashMap<>();
            //1.生成UUID
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //2.生成UserCode
            String userCode = Const.USER_CODE;
            //3.生成token
            String token = TokenUtils.getToken(userCode, uuid);
            resMap.put("token", token);
            resMap.put("userCode", userCode);
            return JSONResult.ok(resMap);
        } catch (Exception e) {
            e.printStackTrace();
            logger.warn("——————————login—用户登录—" + e.toString() + "————————————");
        }
        return JSONResult.errorMsg("登录失败");
    }

}
